package com.yxhl.zoume.core.func.appraisal.ui.fragment;

import android.databinding.DataBindingUtil;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yxhl.zoume.AppraisalDataBinding;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseDataBindingFragment;
import com.yxhl.zoume.core.func.appraisal.model.AppraisalDriverInfo;
import com.yxhl.zoume.core.func.appraisal.model.CommentOnDriverEvent;
import com.yxhl.zoume.core.func.appraisal.presenter.AppraisalLabelPresenter;
import com.yxhl.zoume.core.func.appraisal.presenter.AppraisalPresenter;
import com.yxhl.zoume.core.func.appraisal.ui.activity.AppraisalActivity;
import com.yxhl.zoume.core.func.appraisal.ui.adapter.AppraisalLabelAdapter;
import com.yxhl.zoume.core.func.appraisal.view.AppraisalLabelView;
import com.yxhl.zoume.core.func.appraisal.view.AppraisalView;
import com.yxhl.zoume.data.http.model.appraisal.AppraisalLabel;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.appraisal.AppraisalComponent;
import com.yxhl.zoume.utils.DensityUtils;
import com.yxhl.zoume.utils.PromptUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by vhawk on 16/9/18.
 */
public class AppraisalFragment extends BaseDataBindingFragment implements AppraisalLabelView, AppraisalView {

    private static final String KEY_APPRAISAL_PARAM = "APPRAISAL_PARAM";
    private static final String TAG = "AppraisalFragment";
    @Inject
    AppraisalPresenter appraisalPresenter;
    @Inject
    AppraisalLabelPresenter appraisalLabelPresenter;
    @BindView(R.id.recycle_appraisal)
    RecyclerView appraisalLabelRecycleView;
    @BindView(R.id.iv_comment_1)
    ImageView appraisalIv1;
    @BindView(R.id.iv_comment_2)
    ImageView appraisalIv2;
    @BindView(R.id.iv_comment_3)
    ImageView appraisalIv3;
    @BindView(R.id.iv_comment_4)
    ImageView appraisalIv4;
    @BindView(R.id.iv_comment_5)
    ImageView appraisalIv5;
    @BindView(R.id.bt_appraisal)
    Button appraisalBt;
    @BindView(R.id.tv_passenger_input_index)
    TextView inputIndexTv;
    @BindView(R.id.et_passenger_appraisal_content)
    EditText appraisalContent;
    private AppraisalDataBinding appraisalDataBinding;
    private AppraisalComponent component;
    private String driverId;
    private int judgePoint;
    private AppraisalLabelAdapter adapter;
    private AppraisalDriverInfo driverInfo;

    public static Fragment newInstance(AppraisalDriverInfo driverInfo) {
        Fragment fragment = new AppraisalFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(KEY_APPRAISAL_PARAM, driverInfo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        handleExtraParam();
        appraisalLabelPresenter.attachView(this);
        appraisalLabelPresenter.fetchAppraisalLabel();
        appraisalPresenter.attachView(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        appraisalLabelPresenter.onResume();
        appraisalPresenter.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        appraisalLabelPresenter.onPause();
        appraisalPresenter.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        appraisalLabelPresenter.onDestroy();
        appraisalPresenter.onDestroy();
    }

    @Override
    public void handleFetchLabelSucceed(List<AppraisalLabel> labels) {
        GridLayoutManager girdLayoutManager = new GridLayoutManager(component.context(), 3);
        adapter = new AppraisalLabelAdapter(labels);
        appraisalLabelRecycleView.setAdapter(adapter);
        appraisalLabelRecycleView.setLayoutManager(girdLayoutManager);
        appraisalLabelRecycleView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(DensityUtils.dip2px(component.context(), 2),
                        DensityUtils.dip2px(component.context(), 2),
                        DensityUtils.dip2px(component.context(), 2),
                        DensityUtils.dip2px(component.context(), 2));
            }
        });
    }

    @OnClick(R.id.iv_comment_1)
    public void firstStarClick() {
        showAppraisalStar(1);
    }

    @OnClick(R.id.iv_comment_2)
    public void secondStarClick() {
        showAppraisalStar(2);
    }

    @OnClick(R.id.iv_comment_3)
    public void thirdStarClick() {
        showAppraisalStar(3);
    }

    @OnClick(R.id.iv_comment_4)
    public void forthStarClick() {
        showAppraisalStar(4);
    }

    @OnClick(R.id.iv_comment_5)
    public void fifthStarClick() {
        showAppraisalStar(5);
    }

    private void showAppraisalStar(int star) {
        appraisalBt.setClickable(true);
        judgePoint = star;
        appraisalIv1.setImageResource(star > 0 ? R.drawable.ic_appraisal_selected : R.drawable.ic_appraisal_unselected);
        appraisalIv2.setImageResource(star > 1 ? R.drawable.ic_appraisal_selected : R.drawable.ic_appraisal_unselected);
        appraisalIv3.setImageResource(star > 2 ? R.drawable.ic_appraisal_selected : R.drawable.ic_appraisal_unselected);
        appraisalIv4.setImageResource(star > 3 ? R.drawable.ic_appraisal_selected : R.drawable.ic_appraisal_unselected);
        appraisalIv5.setImageResource(star > 4 ? R.drawable.ic_appraisal_selected : R.drawable.ic_appraisal_unselected);
    }

    @OnClick(R.id.bt_appraisal)
    public void appraisalClick() {
        appraisalPresenter.appraisal(driverId, judgePoint, adapter.getLabelList(), appraisalContent.getText().toString());
    }

    @OnTextChanged(R.id.et_passenger_appraisal_content)
    public void onContentChanged(Editable editable) {
        inputIndexTv.setText(editable.toString().length() + "/60");
    }

    @Override
    public void handleFetchLabelFailure(String msg) {
        PromptUtils.showShort(component.context(), msg);
    }

    @Override
    public void handleFetchLabelError() {
        PromptUtils.showShort(component.context(), R.string.net_error);
    }

    @Override
    public void handleAppraisalSucceed() {
        RxBus.getInstance().send(new CommentOnDriverEvent(judgePoint));
        getAppComponent().navigator().navigateToAppraisalResult((AppraisalActivity) component.context(), driverInfo);
        ((AppraisalActivity) component.context()).finish();
    }

    @Override
    public void handleAppraisalFailure(String msg) {
        PromptUtils.showShort(component.context(), msg);
    }

    @Override
    public void handleAppraisalError() {
        PromptUtils.showShort(component.context(), R.string.net_error);
    }

    private void handleExtraParam() {
        Bundle arguments = getArguments();
        driverInfo = (AppraisalDriverInfo) arguments.getSerializable(KEY_APPRAISAL_PARAM);
        driverId = driverInfo.getOrderId();
        appraisalDataBinding.setDriverInfo(driverInfo);
    }

    @Override
    protected void initializeInjector() {
        component = getComponent(AppraisalComponent.class);
        component.inject(this);
    }

    @Override
    protected View getContentLayout(LayoutInflater inflater, ViewGroup container) {
        appraisalDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_appraisal, container, false);
        return appraisalDataBinding.getRoot();
    }

    @Override
    protected void setupToolBar() {
        AppraisalActivity activity = (AppraisalActivity) mActivity;
        activity.setToolbarTitle("评价");
    }
}
